package shoppingcart.fragment.adapter;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.xfengshopping.R;

import java.util.ArrayList;
import java.util.List;

import shoppingcart.fragment.model.OrderItem;

public class OrderItemAdapter extends RecyclerView.Adapter<OrderItemAdapter.ViewHolder> {
    private final List<OrderItem> items;

    public OrderItemAdapter(List<OrderItem> items) {
        this.items = items != null ? items : new ArrayList<>();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.order_item_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (position < 0 || position >= items.size()) {
            return;
        }

        OrderItem item = items.get(position);
        if (item == null) {
            holder.goodsName.setText("商品加载中...");
            holder.goodsPrice.setText("");
            Glide.with(holder.itemView.getContext())
                    .load(R.drawable.ic_placeholder)
                    .into(holder.goodsImg);
            return;
        }

        try {
            holder.goodsName.setText(item.getGoodsName() != null ? item.getGoodsName() : "未知商品");
            holder.goodsPrice.setText(String.format("¥%d", item.getSellingPrice()));

            String imageUrl = item.getGoodsCoverImg();
            if (!TextUtils.isEmpty(imageUrl)) {
                Glide.with(holder.itemView.getContext())
                        .load(imageUrl)
                        .placeholder(R.drawable.ic_placeholder)
                        .error(R.drawable.ic_error)
                        .into(holder.goodsImg);
            } else {
                Glide.with(holder.itemView.getContext())
                        .load(R.drawable.ic_placeholder)
                        .into(holder.goodsImg);
            }
        } catch (Exception e) {
            Log.e("OrderItemAdapter", "绑定数据异常", e);
        }
    }

    @Override
    public int getItemCount() {
        return items != null ? items.size() : 0;
    }

    // 添加更新数据的方法
    public void updateData(List<OrderItem> newItems) {
        // 清空现有数据
        this.items.clear();

        // 添加新数据
        if (newItems != null) {
            this.items.addAll(newItems);
        }

        // 通知适配器数据已更改
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView goodsImg;
        TextView goodsName;
        TextView goodsPrice;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            goodsImg = itemView.findViewById(R.id.goodsImg);
            goodsName = itemView.findViewById(R.id.goodsname);
            goodsPrice = itemView.findViewById(R.id.goodsprice);
        }
    }
}